package k2;

public class Dynamic {
	public int[] array;
	public int elements = 0;
	
	public void create(int n){
		array = new int[n];
	}
	public void add(int x){
		if(array.length <= elements + 1){
			increase();
		}
		array[elements] = x;
		elements++;
	}
	public int get(int i){
		return array[i];
	}
	public void put(int x, int i){
		array[i] = x;
	}
	public int len(){
		return elements;
	}
	public int rem(){
		int last = array[elements];
		elements--;
		if(elements < array.length / 4){
			reduced();
		}
		return last;
	}
	private void increase(){
		int[] increased = new int[array.length * 2];
		for(int i = 0;i<array.length;i++){
			increased[i] = array[i];
		}
		array = increased;
	}
	private void reduced(){
		int[] reduced = new int[array.length / 2];
		for(int i = 0;i<reduced.length;i++){
			reduced[i] = array[i];
		}
		array = reduced;
	}
}	
